package com.google.android.play.core.assetpacks;

import android.content.Context;
import c.b.a.c.a.c.a;
import c.b.a.c.a.c.y;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

final class b0 {

    /* renamed from: c  reason: collision with root package name */
    private static final a f2463c = new a("AssetPackStorage");

    /* renamed from: a  reason: collision with root package name */
    private final Context f2464a;

    /* renamed from: b  reason: collision with root package name */
    private final e2 f2465b;

    static {
        TimeUnit.DAYS.toMillis(14);
        TimeUnit.DAYS.toMillis(28);
    }

    b0(Context context, e2 e2Var) {
        this.f2464a = context;
        this.f2465b = e2Var;
    }

    private final File A(String str, int i, long j) {
        return new File(k(str, i, j), "merge.tmp");
    }

    private final List<File> B() {
        ArrayList arrayList = new ArrayList();
        try {
            if (!E().exists() || E().listFiles() == null) {
                return arrayList;
            }
            for (File file : E().listFiles()) {
                if (!file.getCanonicalPath().equals(C().getCanonicalPath())) {
                    arrayList.add(file);
                }
            }
            return arrayList;
        } catch (IOException e) {
            f2463c.e("Could not process directory while scanning installed packs. %s", e);
        }
    }

    private final File C() {
        return new File(E(), "_tmp");
    }

    private final File D(String str, int i, long j) {
        return new File(new File(new File(C(), str), String.valueOf(i)), String.valueOf(j));
    }

    private final File E() {
        return new File(this.f2464a.getFilesDir(), "assetpacks");
    }

    private final File b(String str, int i) {
        return new File(z(str), String.valueOf(i));
    }

    private static void e(File file) {
        if (file.listFiles() != null && file.listFiles().length > 1) {
            long h = h(file);
            for (File file2 : file.listFiles()) {
                if (!file2.getName().equals(String.valueOf(h)) && !file2.getName().equals("stale.tmp")) {
                    n(file2);
                }
            }
        }
    }

    private static long h(File file) {
        if (file.exists()) {
            ArrayList arrayList = new ArrayList();
            try {
                for (File file2 : file.listFiles()) {
                    if (!file2.getName().equals("stale.tmp")) {
                        arrayList.add(Long.valueOf(file2.getName()));
                    }
                }
            } catch (NumberFormatException e) {
                f2463c.d(e, "Corrupt asset pack directories.", new Object[0]);
            }
            if (!arrayList.isEmpty()) {
                Collections.sort(arrayList);
                return ((Long) arrayList.get(arrayList.size() - 1)).longValue();
            }
        }
        return -1;
    }

    private static boolean n(File file) {
        File[] listFiles = file.listFiles();
        boolean z = true;
        if (listFiles != null) {
            for (File n : listFiles) {
                if (!n(n)) {
                    z = false;
                }
            }
        }
        if (file.delete()) {
            return z;
        }
        return false;
    }

    private static long p(File file) {
        if (!file.isDirectory()) {
            return file.length();
        }
        File[] listFiles = file.listFiles();
        long j = 0;
        if (listFiles != null) {
            for (File p : listFiles) {
                j += p(p);
            }
        }
        return j;
    }

    private final File z(String str) {
        return new File(E(), str);
    }

    /* access modifiers changed from: package-private */
    public final long a() {
        return p(E());
    }

    /* access modifiers changed from: package-private */
    public final File c(String str, int i, long j) {
        return new File(b(str, i), String.valueOf(j));
    }

    /* access modifiers changed from: package-private */
    public final File d(String str, int i, long j, String str2) {
        return new File(new File(new File(D(str, i, j), "_slices"), "_unverified"), str2);
    }

    /* access modifiers changed from: package-private */
    public final void f(String str, int i, long j, int i2) {
        File A = A(str, i, j);
        Properties properties = new Properties();
        properties.put("numberOfMerges", String.valueOf(i2));
        A.getParentFile().mkdirs();
        A.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(A);
        properties.store(fileOutputStream, (String) null);
        fileOutputStream.close();
    }

    /* access modifiers changed from: package-private */
    public final void g(List<String> list) {
        int a2 = this.f2465b.a();
        List<File> B = B();
        int size = B.size();
        for (int i = 0; i < size; i++) {
            File file = B.get(i);
            if (!list.contains(file.getName()) && h(file) != ((long) a2)) {
                n(file);
            }
        }
    }

    /* access modifiers changed from: package-private */
    public final File i(String str, int i, long j) {
        return new File(c(str, i, j), "_metadata");
    }

    /* access modifiers changed from: package-private */
    public final File j(String str, int i, long j, String str2) {
        return new File(new File(new File(D(str, i, j), "_slices"), "_verified"), str2);
    }

    /* access modifiers changed from: package-private */
    public final File k(String str, int i, long j) {
        return new File(D(str, i, j), "_packs");
    }

    /* access modifiers changed from: package-private */
    public final File l(String str, int i, long j, String str2) {
        return new File(w(str, i, j, str2), "checkpoint.dat");
    }

    /* access modifiers changed from: package-private */
    public final void m() {
        List<File> B = B();
        int size = B.size();
        for (int i = 0; i < size; i++) {
            File file = B.get(i);
            if (file.listFiles() != null) {
                e(file);
                long h = h(file);
                if (((long) this.f2465b.a()) != h) {
                    try {
                        new File(new File(file, String.valueOf(h)), "stale.tmp").createNewFile();
                    } catch (IOException unused) {
                        f2463c.e("Could not write staleness marker.", new Object[0]);
                    }
                }
                for (File e : file.listFiles()) {
                    e(e);
                }
            }
        }
    }

    /* access modifiers changed from: package-private */
    public final int o(String str, int i, long j) {
        File A = A(str, i, j);
        if (!A.exists()) {
            return 0;
        }
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream(A);
        try {
            properties.load(fileInputStream);
            fileInputStream.close();
            if (properties.getProperty("numberOfMerges") != null) {
                try {
                    return Integer.parseInt(properties.getProperty("numberOfMerges"));
                } catch (NumberFormatException e) {
                    throw new v0("Merge checkpoint file corrupt.", (Exception) e);
                }
            } else {
                throw new v0("Merge checkpoint file corrupt.");
            }
        } catch (Throwable th) {
            y.a(th, th);
        }
        throw th;
    }

    /* access modifiers changed from: package-private */
    public final File q(String str, int i, long j, String str2) {
        return new File(w(str, i, j, str2), "checkpoint_ext.dat");
    }

    /* access modifiers changed from: package-private */
    public final boolean r(String str) {
        if (z(str).exists()) {
            return n(z(str));
        }
        return true;
    }

    /* access modifiers changed from: package-private */
    public final int s(String str) {
        return (int) h(z(str));
    }

    /* access modifiers changed from: package-private */
    public final File t(String str, int i, long j) {
        return new File(new File(D(str, i, j), "_slices"), "_metadata");
    }

    /* access modifiers changed from: package-private */
    public final File u(String str, int i, long j, String str2) {
        return new File(w(str, i, j, str2), "slice.zip");
    }

    /* access modifiers changed from: package-private */
    public final long v(String str) {
        return h(b(str, s(str)));
    }

    /* access modifiers changed from: package-private */
    public final File w(String str, int i, long j, String str2) {
        return new File(t(str, i, j), str2);
    }

    /* access modifiers changed from: package-private */
    public final void x() {
        n(E());
    }

    /* access modifiers changed from: package-private */
    public final void y(String str, int i, long j) {
        if (D(str, i, j).exists()) {
            n(D(str, i, j));
        }
    }
}
